#include "laurin-ip-network.h"


LaurinIPNetwork::LaurinIPNetwork(u_int32_t address, u_int32_t subnetMask)
{
	_address = address;
	_mask = subnetMask	;
	_range = LaurinIPRange::getIPRange(address, subnetMask);
}


LaurinIPNetwork::~LaurinIPNetwork()
{
	if(_range != NULL)
		delete _range;
}


u_int32_t LaurinIPNetwork::getNetworkAddress() const
{
	return _address;
}


u_int32_t LaurinIPNetwork::getNetworkSubnetMask() const
{
	return _mask;
}


LaurinIPRange* LaurinIPNetwork::getNetworkIPRange() const
{
	return new LaurinIPRange(*_range);
}



void LaurinIPNetwork::addClient(const LaurinNetworkClient& client, bool checkPresent)
{
	if(checkPresent && isClientPresent(client.getIP()))
		return;

	_clients.push_front(client);
}


const std::list<LaurinNetworkClient>& LaurinIPNetwork::getClients() const
{
	return _clients;
}


bool LaurinIPNetwork::isClientPresent(u_int32_t ip)
{
	std::list<LaurinNetworkClient>::iterator b = _clients.begin();
	std::list<LaurinNetworkClient>::iterator e = _clients.end();

	while(b != e)
	{
		if((*b).getIP() == ip)
			return true;

		b++;
	}

	return false;
}


int LaurinIPNetwork::getNumberOfClients() const
{
	return _clients.size();
}
